package com.luhuiguo.cms.server.handler;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Component;

import com.luhuiguo.cms.server.service.ArticleService;
import com.luhuiguo.cms.server.util.MapperUtils;
import com.luhuiguo.cms.shared.model.ArticleModel;
import com.luhuiguo.cms.shared.rpc.GetArticle;
import com.luhuiguo.cms.shared.rpc.GetArticleResult;
import com.luhuiguo.gwt.dispatcher.server.ActionHandler;
import com.luhuiguo.gwt.dispatcher.server.ExecutionContext;
import com.luhuiguo.gwt.dispatcher.shared.exception.ActionException;

@Component
public class GetArticleHandler implements ActionHandler<GetArticle, GetArticleResult> {


    @Autowired
    private ArticleService articleService;

    @Override
    @PreAuthorize("isAuthenticated()")
    public GetArticleResult execute(GetArticle action, ExecutionContext context) throws ActionException {
        ArticleModel model = MapperUtils.map(articleService.getArticle(action.get()), ArticleModel.class);
        return new GetArticleResult(model);
    }

    @Override
    public Class<GetArticle> getActionType() {
        return GetArticle.class;
    }

    @Override
    public void rollback(GetArticle action, GetArticleResult result, ExecutionContext context) throws ActionException {

    }

}
